﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace NF_Tools
{
    public class MenuControl : RadioButton, IContainerControl
    {
        [Category("NanQi"), Description("图标")]
        public Image MenuIcon
        {
            get { return this.Image; }
            set { this.Image = value; }
        }

        [Category("NanQi"), Description("标题")]
        public string MenuTitle
        {
            get { return this.Text; }
            set { this.Text = value; }
        }




        #region field
        private Control _activeControl;

        private float _countFontSize = 24;
        private Font _countFont = null;

        private float _descFontSize = 16;
        private Font _descFont = null;

        private bool _isHovered = false;
        private bool _isPressed = false;
        private bool _isFocused = false;

        private Point _mouseLocation = Point.Empty;

        private int _borderWidth = 4;
        private Color _borderColor;

        #endregion

        public MenuControl()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint |
                    ControlStyles.OptimizedDoubleBuffer |
                    ControlStyles.ResizeRedraw |
                    ControlStyles.UserPaint, true);

            this.Width = 160;
            this.Height = 80;
            this.BackColor = Color.Transparent;
            this.Font = new Font("Segoe UI", 18, FontStyle.Bold, GraphicsUnit.Pixel);
            this.ForeColor = Color.FromArgb(176, 176, 176);
            
            this.ImageAlign = ContentAlignment.MiddleLeft;
            this.TextAlign = ContentAlignment.MiddleRight;
            this.CheckAlign = ContentAlignment.TopCenter;

            _borderColor = Color.FromArgb(176, 176, 176);

            //this.SizeChanged += (s, e) =>
            //{
            //    this.Width = 160;
            //    this.Height = 80;
            //};
        }

        protected override void InitLayout()
        {
            this.AutoSize = false;
            base.InitLayout();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            if (_isPressed || Checked)
            {
                this.ForeColor = Color.FromArgb(90, 90, 90);
            }
            else
            {
                this.ForeColor = Color.FromArgb(176, 176, 176);
            }

            base.OnPaint(e);

            using (var pen = new Pen(this.Parent.BackColor, 32))
            {

                e.Graphics.DrawLine(pen, 0, 0, this.Width, 0);
            }
        }

        #region Focus Methods

        protected override void OnGotFocus(EventArgs e)
        {
            _isFocused = true;
            Invalidate();

            base.OnGotFocus(e);
        }

        protected override void OnLostFocus(EventArgs e)
        {
            _isFocused = false;
            _isHovered = false;
            _isPressed = false;
            Invalidate();

            base.OnLostFocus(e);
        }

        protected override void OnEnter(EventArgs e)
        {
            _isFocused = true;
            Invalidate();

            base.OnEnter(e);
        }

        protected override void OnLeave(EventArgs e)
        {
            _isFocused = false;
            _isHovered = false;
            _isPressed = false;
            Invalidate();

            base.OnLeave(e);
        }

        #endregion

        #region IContainerControl 成员

        public bool ActivateControl(Control active)
        {
            if (Controls.Contains(active))
            {
                active.Select();
                _activeControl = active;

                return true;
            }

            return false;
        }

        public Control ActiveControl
        {
            get { return _activeControl; }
            set { _activeControl = value; }
        }

        #endregion

        #region Keyboard Methods

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Space)
            {
                _isHovered = true;
                _mouseLocation = Point.Empty;
                _isPressed = true;
                Invalidate();
            }

            base.OnKeyDown(e);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            _isHovered = false;
            _isPressed = false;
            Invalidate();

            base.OnKeyUp(e);
        }

        #endregion

        #region Mouse Methods

        protected override void OnMouseEnter(EventArgs e)
        {
            _isHovered = true;
            Invalidate();

            base.OnMouseEnter(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseLocation = e.Location;
                _isPressed = true;
                Invalidate();
            }

            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            _isPressed = false;
            Invalidate();

            base.OnMouseUp(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            _isHovered = false;
            Invalidate();

            base.OnMouseLeave(e);
        }

        #endregion
    }
}
